package com.yanqu.road.dao.impl.kowloon;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.kowloon.KowloonHeroData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class KowloonHeroDataDaoImpl extends TempDataObjectDao<KowloonHeroData> {

    public static String getKowloonHeroDataSql = "select * from `t_u_kowloon_hero_data` where `userId`=?";

    public Map<Integer, KowloonHeroData> getKowloonHeroDataMap(long userId) {
        Map<Integer, KowloonHeroData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getKowloonHeroDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                KowloonHeroData heroData = parse(rs);
                map.put(heroData.getHeroId(), heroData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateKowloonHeroDataSql = "update `t_u_kowloon_hero_data` set level=?,attributeData=?,heroIdx=?,updateTime=? where userId=? and heroId=?";

    @Override
    protected boolean updateData(KowloonHeroData kowloonHeroData) {
        kowloonHeroData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, kowloonHeroData.getLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, ""));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonHeroData.getHeroIdx()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonHeroData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonHeroData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonHeroData.getHeroId()));
        if (getDb().executeNoneQuery(updateKowloonHeroDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertKowloonHeroDataSql = "insert into `t_u_kowloon_hero_data`(userId,heroId,level,attributeData,heroIdx,updateTime) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(KowloonHeroData kowloonHeroData) {
        kowloonHeroData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, kowloonHeroData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonHeroData.getHeroId()));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonHeroData.getLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, ""));
        para.put(i++, new DbParameter(Types.INTEGER, kowloonHeroData.getHeroIdx()));
        para.put(i++, new DbParameter(Types.BIGINT, kowloonHeroData.getUpdateTime()));
        if (getDb().executeNoneQuery(insertKowloonHeroDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected KowloonHeroData parseData(ResultSet rs) throws SQLException {
        KowloonHeroData kowloonHeroData = new KowloonHeroData();
        kowloonHeroData.setUserId(rs.getLong("userId"));
        kowloonHeroData.setHeroId(rs.getInt("heroId"));
        kowloonHeroData.setLevel(rs.getInt("level"));
        // 属性字段不入库
//        String attributeData = rs.getString("attributeData");
//        if (!StringUtils.isNullOrEmpty(attributeData)) {
//            kowloonHeroData.setAttributeData(JSON.parseObject(attributeData, new TypeReference<KowloonNpcAttributeData>() {
//            }));
//        }
        kowloonHeroData.setHeroIdx(rs.getInt("heroIdx"));
        kowloonHeroData.setUpdateTime(rs.getLong("updateTime"));

        return kowloonHeroData;
    }

}
